package com.dq.controller.app.goods;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.goods.stock.OrgGoodsAndStockQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.domain.category.Category;
import com.dq.domain.category.NullCategory;
import com.dq.domain.goods.Goods;
import com.dq.domain.goods.Product;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.service.AllService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author liuhui
 * @date 2019/11/27 0027 14:46:52
 */
@RestController
@RequestMapping("/app/categoryStock/")
@Api(description = "分类库存管理")
public class AppCategoryStockController extends AllService {

    @PostMapping("/list")
    @ApiOperation("商品分类统计库存列表")
    public Vo list(@RequestBody OrgGoodsAndStockQuery orgGoodsStockQuery)throws AdminException {
        // 关联门店, 门店ID, 不传默认是登录账号门店下
        long orgId = orgGoodsStockQuery.getOrgId() == null ? sysUserService.getUserInSection().getSection().getId() : orgGoodsStockQuery.getOrgId();

        JSONArray array = new JSONArray();

        Map<String, Object> query = orgGoodsStockQuery.parseMap();

        List<Category> categoryList = new ArrayList<>();
        if(orgGoodsStockQuery.getCategoryId() == null) {
            List<NullCategory> categories = nullCategoryRepository.findAll();
            List<Category> categoryListPar = categoryRepository.findByParentCategoryIdAndDeletedFalseOrderBySortNoAsc(categories.get(0).getId());

            query.put("parentCategory" + BaseDaoImpl.inSqlStr, categoryListPar);

            categoryList = dao.find(dao.query2Hql(Category.class, query), query);
        }else {
            query.put("parentCategory_id", orgGoodsStockQuery.getCategoryId());
            query.remove("categoryId");
            categoryList = dao.find(dao.query2Hql(Category.class, query), query);
        }
        if(categoryList.size() > 0) {
            for (Category category : categoryList) {
                JSONObject cateJson = new JSONObject();

                orgGoodsStockQuery.setCategoryId(category.getId());
                orgGoodsStockQuery.setPage(null);
                orgGoodsStockQuery.setSize(null);
                List<Goods> goodsList = goodsService.goodsByOrgAndStockList(orgGoodsStockQuery, orgId, false);
                Long stock = 0L;
                for (Goods goods : goodsList) {
                    stock = stock + goods.getGoodsStock();
                }
                int count = goodsList.size();
                cateJson.put("count", count + " 种");
                cateJson.put("stock", "库存 " + stock);
                cateJson.put("name", category.getTitle());
                cateJson.put("categoryId", category.getId());
                array.add(cateJson);
            }
        }

        return new ListDataVo(array);
    }

    /**
     * 根据product 返回数据
     */
    @PostMapping("/infolist")
    @ApiOperation("商品库存分类详情 列表")
    public Vo infolist(@RequestBody @ApiParam("只需 categoryId") OrgGoodsAndStockQuery orgGoodsStockQuery)throws AdminException {
        SysUser user = sysUserService.getUser();
        if(user.getSection() == null || user.getSection().getId() == null) {
            throw new AdminException("门店ID为空");
        }
        if(orgGoodsStockQuery.getCategoryId() == null) {
            throw new AdminException("分类id为空");
        }
        orgGoodsStockQuery.format();
        List<Product> productList = goodsService.productAndStockList(orgGoodsStockQuery,  user.getSection().getId(), false);
        Long totalCount =  goodsService.productAndStockCount(orgGoodsStockQuery,  user.getSection().getId(), false);
        List<JSONObject> list = new ArrayList<>();
        for (Product product : productList) {
            list.add(product.toStockVO());
        }

        return new ListDataVo(list, totalCount);
    }

}
